package krustyproject;

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

import java.sql.Date;
import java.util.ArrayList;

import krustyproject.PalletPane.DateSelectionListener;
import krustyproject.PalletPane.NameSelectionListener;
import krustyproject.PalletPane.PalletSelectionListener;

/**
 * The GUI pane where a new user logs in. Contains a text field where the user
 * id is entered and a button to log in.
 */
public class SearchPane extends BasicPane {
	private static final long serialVersionUID = 1;
	/**
	 * The text field where the user id is entered.
	 */
	private JTextField[] fields;
	
	private DefaultListModel nameListModel;
	
	private JList nameList;

	/**
	 * The number of the field where the user id is entered.
	 */
	private static final int DATE1 = 0;
	
	private static final int DATE2 = 1;
	
	

	/**
	 * The total number of fields in the fields array.
	 */
	private static final int NBR_FIELDS = 2;

	/**
	 * Create the login pane.
	 * 
	 * @param db
	 *            The database object.
	 */
	public SearchPane(Database db) {
		super(db);
	}

	/**
	 * Create the top panel, consisting of the text field.
	 * 
	 * @return The top panel.
	 */
	public JComponent createTopPanel() {
		String[] texts = new String[NBR_FIELDS];
		texts[DATE1] = "From(YYYY-MM-DD): ";
		texts[DATE2] = "To(YYYY-MM-DD): ";
		fields = new JTextField[NBR_FIELDS];
		fields[DATE1] = new JTextField(20);
		fields[DATE2] = new JTextField(20);
		return new InputPanel(texts, fields);
	}
	
	
	public JComponent createLeftPanel() {
		nameListModel = new DefaultListModel();

		nameList = new JList(nameListModel);
		nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		nameList.setPrototypeCellValue("123456789012");
		
		// DENNA KAN VARA FEL
		nameList.addListSelectionListener(new NameSelectionListener());
		JScrollPane p1 = new JScrollPane(nameList);

		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1, 2));
		p.add(p1);
		return p;
	}

	/**
	 * Create the bottom panel, consisting of the login button and the message
	 * line.
	 * 
	 * @return The bottom panel.
	 */
	public JComponent createBottomPanel() {
		JButton[] buttons = new JButton[1];
		buttons[0] = new JButton("Search Pallets By Date");
		ActionHandler actHand = new ActionHandler();
		fields[DATE1].addActionListener(actHand);
		return new ButtonAndMessagePanel(buttons, messageLabel, actHand);
	}

	/**
	 * Perform the entry actions of this pane, i.e. clear the message line.
	 */
	public void entryActions() {
		clearMessage();
		fillNameList();
	}
	
	private void fillNameList() {
		nameListModel.removeAllElements();
		ArrayList<String> a = db.getCookies();
		for (int i = 0; i < a.size(); i++) {
			nameListModel.addElement(a.get(i));
		}
	}
	
	private void fillResultList(String date1, String date2, String cookie){
		nameListModel.removeAllElements();
		ArrayList<Integer> a = db.getPalletsBetweenDates(cookie, date1, date2);
			for (int i = 0; i < a.size(); i++) {

				nameListModel.addElement(a.get(i));	
			}	
	}

	/**
	 * A class which listens for button clicks.
	 */
	class ActionHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String date1 = fields[DATE1].getText();
			String date2 = fields[DATE2].getText();
			String cookie = (String) nameList.getSelectedValue();
			fillResultList(date1, date2, cookie);
			
		}
	}
	
	class NameSelectionListener implements ListSelectionListener {

		public void valueChanged(ListSelectionEvent e) {

		}

	}
}
